package com.nidu.demo.mail.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.mail.gateway.MailAccountGateway;
import com.nidu.demo.mail.model.MailAccount;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * 邮件账号删除命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailAccountDeleteCmdExe {

    private final MailAccountGateway gateway;

    @Transactional
    public Response execute(Long id) {
        // 1. 检查邮件账号是否存在
        MailAccount existing = gateway.getById(id);
        if (existing == null) {
            throw new BizException("邮件账号不存在");
        }

        // 2. 删除邮件账号
        gateway.deleteById(id);

        return Response.buildSuccess();
    }

    @Transactional
    public Response execute(Set<Long> ids) {
        // 批量删除邮件账号
        gateway.deleteByIds(ids);
        return Response.buildSuccess();
    }

}
